package com.m1905hd.user.service.impl

import com.m1905hd.user.entity.User
import com.m1905hd.user.service.RoleService
import com.m1905hd.user.service.UserService
import com.m1905hd.user.utils.PasswordHelper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/3/31
 */
@Service("userService")
class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    static String NAMESPACE = (User.class.getName() + ".").intern();
    @Autowired
    RoleService roleService


    @Override
    boolean changePassword(int id, String newPassword) {
        return false
    }


    @Override
    User findByUserName(String name) {
        return getSqlSession().selectOne(NAMESPACE + "findByUserName", name)
    }

    @Override
    Set<String> findRoles(String userName) {
        User user = findByUserName(userName);
        if (user == null) {
            return Collections.EMPTY_SET;
        }
    }

    @Override
    Set<String> findPermissions(String userName) {
        User user = findByUserName(userName);
        if (user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findPermissions(user.getRoleIds());
    }

    @Override
    boolean changeStatus(int id, int available) {
        return getSqlSession().update(NAMESPACE + "changeStatus", [id: id, available: available]) == 1
    }

    @Override
    boolean resetPassword(int id, String password) {
        PasswordHelper.HashPassword hashPassword = PasswordHelper.encryptPassword(password)
        return getSqlSession().update(NAMESPACE + 'resetPassword', [id: id, password: hashPassword.password, salt: hashPassword.salt]) == 1
    }

    @Override
    boolean updateRoles(int id, String roleIds) {
        return getSqlSession().update(NAMESPACE + 'updateRoles', [id: id, roleIds: roleIds]) == 1

    }

    @Override
    String getNameSpace() {
        return NAMESPACE
    }

}
